<style scoped>
.submit_button{background: var(--color-primary);color: #fff;}
.submit{padding: 14px;}
.submit .tips{font-size: 0.8em;line-height: 1.5;padding-bottom: 12px;}
.submit .tips .red{color: var(--van-danger-color);}
</style>
<template>
  <HeaderSlot>
    <van-nav-bar :title="$t('提现')" left-arrow @click-left="href_handle(-1)" />
  </HeaderSlot>
  <van-tabs v-model:active="type">
    <van-tab v-for="(row, key) in form_config_map" :name="key" :title="label_map[key]">
      <form_param :ref="(el)=>{this.form_refs[key] = el;}" :config="row"></form_param>
    </van-tab>
  </van-tabs>
  <div class="submit">
    <div class="tips">
      <p>当前可提现金额(元)：{{ can_withdraw }}</p>
      <p class="red">提现手续费扣除{{service_money_text}}(含个人所得税)</p>
    </div>
    <div>
      <van-button round block text="提现" @click="submit()" class="submit_button"/>
    </div>
  </div>
</template>

<script>
import form_param from "@/components/form_param.vue";
import {ajax} from "@/js/api_tool.js";
import router_auto, {href_handle} from "@/vue_plugin/router_auto.js";
import {msg} from "@/js/vant_tool.js";
import HeaderSlot from "@/components/slot/HeaderSlot.vue";
import {use_config_map} from "@/store/config_map.js";

export default {
  components: {HeaderSlot, form_param},
  data(){
    return {
      type: '',
      label_map: {wechat:'微信', alipay:'支付宝', bank:'银行卡'},
      form_config_map: {},
      form_refs: {},

      withdraw_min: Number(use_config_map().config.user?.withdraw?.withdraw_min),
      can_withdraw: 0,
      service_money_text: '',
    }
  },
  methods:{
    href_handle,
    init(old_data){
      let form_map = {
        wechat: [
          {name:'code', label:'微信号', type:'text_one', tips:'收款的微信号'},
          {name:'truename', label:'真实姓名', type:'text_one', tips:'微信号对应的用户姓名'},
        ],
        alipay: [
          {name:'code', label:'支付宝账户', type:'text_one', tips:'收款的支付宝账户'},
          {name:'truename', label:'真实姓名', type:'text_one', tips:'支付宝账户对应的用户姓名'},
        ],
        bank: [
          {name:'code', label:'支付宝账户', type:'text_one', tips:'收款的支付宝账户'},
          {name:'truename', label:'真实姓名', type:'text_one', tips:'支付宝账户对应的用户姓名'},
          {name:'bank_name', label:'银行名称', type:'select_one', config: use_config_map().config.user?.withdraw?.withdraw_bank_list || [], tips:''},
          {name:'bank_address', label:'支行地址', type:'text_one', tips:''},
        ]
      };
      let form_common = [
        {name:'money', label:'提现', type:'text_one', tips:`最低提现${this.withdraw_min}元`},
      ];
      let type_allow = use_config_map().config.user?.withdraw?.withdraw_type_allow || [];
      let form_config_map = {};
      (type_allow ? type_allow.split(',') : []).forEach(type=>{
        if (!form_map[type]) return;
        let config = form_map[type].concat(form_common);
        let old_value = old_data[type];
        if (old_value){
          config.forEach(row=>{
            if (old_value[row.name]) row.value = old_value[row.name];
          })
        }
        form_config_map[type] = config
      })
      this.form_config_map = form_config_map;
    },
    submit(){
      let form_config = this.form_config_map[this.type];
      let data = {
        type: this.type,
        need_bill: 0,
        bill_file: '',
      };
      let error_map = {}
      form_config.forEach(row=>{
        data[row.name] = row.value;
        if (!row.value) error_map[row.name] = '不能为空';
      })
      if (data.money > this.can_withdraw) {
        error_map['money'] = '超过可提现金额';
      } else if (data.money < this.withdraw_min){
        error_map['money'] = '最低为：'+this.withdraw_min;
      }
      if (Object.keys(error_map).length){
        this.form_refs[this.type].error(error_map);
        return;
      }
      ajax({url:'/home/user/withdraw_add', data:data}, res=>{
        setTimeout(()=>{router_auto.back();}, 1500)
      })
    },
  },
  computed:{
    form_config(){
      if (!this.type) return [];
      let res = [].concat(this.form_common, this.form_map[this.type]);
    },
  },
  mounted(){
    ajax('/home/user/withdraw_config', res=>{
      if (res.data.real_name_status != 1) {
        msg('需要先完成实名认证');
        href_handle('/user/real_name', 1000, true);
      }

      this.can_withdraw = res.data.canWithdraw;
      let service_money_arr = [];
      if (res.data.withdraw_service_ratio>0) {
        service_money_arr.push(res.data.withdraw_service_ratio+'%')
      }
      if (res.data.withdraw_service_money>0) {
        service_money_arr.push(res.data.withdraw_service_money+'元')
      }
      this.service_money_text = service_money_arr.join('+');

      this.init(res.data.old_data);
      return false;
    })
  }
}
</script>